/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.model;

import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;

import net.sourceforge.perforce.core.resources.RemoteFile;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
  * @version $Revision: 75 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class RemoteFileElement extends PerforceModelElement implements IWorkbenchAdapter
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Message format for creating the label. */
	private MessageFormat format = new MessageFormat("{0} #{1}/{2} <{3}>");
	/** Message format for creating the label (deleted files). */
	private MessageFormat formatDeleted = new MessageFormat("{0} #{1}/{2} <{3}> <-deleted->");
	
	// ---------------------------------------------------------- Public Methods (IWorkbenchAdapter)

	/** @see IWorkbenchAdapter#getChildren(Object) */
	public Object[] getChildren(Object o)
	{
		return new Object[0];
	}

	/** @see IWorkbenchAdapter#getImageDescriptor(Object) */
	public ImageDescriptor getImageDescriptor(Object object)
	{
		if (!(object instanceof RemoteFile)) 
		{
			return null;
		}
		
		RemoteFile file = (RemoteFile) object;
		return PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor(file.getName());
	}

	/** @see IWorkbenchAdapter#getLabel(Object) */
	public String getLabel(Object object)
	{
		if (!(object instanceof RemoteFile)) 
		{
			return null;
		}
		
		final RemoteFile file = (RemoteFile) object;
		final ResourceSyncInfo[] syncInfo = new ResourceSyncInfo[1];
		
		Shell shell = new Shell(Display.getDefault());
		try 
		{
			run(true, false, new IRunnableWithProgress() 
			{
				/** @see IRunnableWithProgress#run(IProgressMonitor) */
				public void run(IProgressMonitor monitor)
					throws InvocationTargetException, InterruptedException 
				{
					syncInfo[0] = file.getSyncInfo(monitor);
				}
			});		
		} 
		catch (InvocationTargetException e) 
		{
			handle(e);
		} 
		catch (InterruptedException e) 
		{
			handle(e);
		}
		finally
		{
			shell.dispose();
		}
				 
		if (syncInfo[0] == null)
		{
			return "!" + file.getName() + "!";
		}
		
		Object[] arguments = new Object[] 
		{ 
			file.getName(), 
			syncInfo[0].getHaveRev(), 
			syncInfo[0].getHeadRev(), 
			syncInfo[0].getHeadType() 
		};
		
		if (syncInfo[0].isDeleted())
		{
			return formatDeleted.format(arguments);
		}
		else
		{
			return format.format(arguments);
		}
	}

	/** @see IWorkbenchAdapter#getParent(Object) */
	public Object getParent(Object o)
	{
		return null;
	}
}
